package com.sav.common.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sav.common.containers.CategoryConteiner;
import com.sav.common.dao.CategoryDAO;
import com.sav.common.localCatalog.Catalog;
import com.sav.common.localCatalog.LocalCategory;




@Controller
public class CategoryController  {
	@Autowired(required = true)
    private CategoryDAO categoryDAO;
    
    @RequestMapping(value = "/editCategory", method = RequestMethod.GET) 
    public String mainMethod(
    		@RequestParam(value="parent", required=true) Integer idParent,
    		ModelMap model,
    		HttpSession httpSession) throws Exception {
    	
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	if (workCatalog == null) {
    		workCatalog = Catalog.getWorkCatalog();
    		httpSession.setAttribute("workCatalog", workCatalog);
    	}
    	
    	
    	List<LocalCategory> listCategory;
    	if (idParent == 0)
    		listCategory = workCatalog.getListParentCategory(); else
    			listCategory = workCatalog.getListSubCategory(idParent);
    		
    	
    	ArrayList<CategoryConteiner> listCategoryConteiner = new ArrayList<CategoryConteiner>();
    	for (LocalCategory c : listCategory) {
    		Boolean b = workCatalog.haveSubCategory(c.getIdCategory());
    		listCategoryConteiner.add(new CategoryConteiner(c.toCategory(), b));
		} 
    	String name;
    	if (idParent == 0) 
    		name = "Root of Category"; else
    			name = workCatalog.getCategoryById(idParent).getName();
    	
    	model.addAttribute("category", listCategoryConteiner);
    	model.addAttribute("idParent", idParent);
    	model.addAttribute("nameParentCategory", name);
    	model.addAttribute("level", workCatalog.getLevelNesting(idParent));
    	
    	
    	/*List<LocalCategory> listCategory;
    	if (idParent == 0)
    		listCategory = Catalog.getInstance().getListParentCategory(); else
    			listCategory = Catalog.getInstance().getListSubCategory(idParent);
    		
    	
    	ArrayList<CategoryConteiner> listCategoryConteiner = new ArrayList<CategoryConteiner>();
    	for (LocalCategory c : listCategory) {
    		Boolean b = Catalog.getInstance().haveSubCategory(c.getIdCategory());
    		listCategoryConteiner.add(new CategoryConteiner(c.toCategory(), b));
		} 
    	String name;
    	if (idParent == 0) 
    		name = "Root of Category"; else
    			name = Catalog.getInstance().getCategoryById(idParent).getName();
    	
    	model.addAttribute("category", listCategoryConteiner);
    	model.addAttribute("idParent", idParent);
    	model.addAttribute("nameParentCategory", name);
    	model.addAttribute("level", Catalog.getInstance().getLevelNesting(idParent));
    	*/
        return "/editCategory"; 
    }
    
    @RequestMapping(value = "/changeCategory", method = RequestMethod.POST)
    public @ResponseBody void changeCategory(
    		@RequestParam(value="id", required=true) Integer id,
    		@RequestParam(value="name", required=true) String name,
    		@RequestParam(value="description", required=true) String description,
    		Model model,
    		HttpSession httpSession)  throws Exception{
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	workCatalog.changeCategory(id, name, description);
	}
    
    @RequestMapping(value = "/swapCategory", method = RequestMethod.POST)
    public @ResponseBody void swapCategory(
    		@RequestParam(value="id1", required=true) Integer id1,
    		@RequestParam(value="id2", required=true) Integer id2,
    		Model model, 
    		HttpSession httpSession)  throws Exception{
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	workCatalog.swapCategory(id1, id2);
	}
    
    @RequestMapping(value = "/deleteCategory", method = RequestMethod.POST)
    public @ResponseBody void changeCategory(
    		@RequestParam(value="id", required=true) Integer id,
    		Model model,
    		HttpSession httpSession)  throws Exception{
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	workCatalog.deleteCategory(id);
	}
    
    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public @ResponseBody int addCategory(
    		@RequestParam(value="name", required=true) String name,
    		@RequestParam(value="description", required=true) String description,
    		@RequestParam(value="idParent", required=true) int idParentCategory,
    		Model model,
    		HttpSession httpSession)  throws Exception{
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	return workCatalog.addCategory(name, description, idParentCategory);
	}
    
    @RequestMapping(value = "/saveChanges", method = RequestMethod.POST)
    public @ResponseBody void saveCategory(
    		Model model,
    		HttpSession httpSession)  throws Exception{
    	Catalog workCatalog = (Catalog) httpSession.getAttribute("workCatalog");
    	workCatalog.saveWorkCatalog();
    	workCatalog = Catalog.getWorkCatalog();
    	httpSession.setAttribute("workCatalog", workCatalog);
	}
}
